/************************************************************************
This file is part of lpc2xxx package for the simpleRTJ virtual machine.

This file is covered by the GNU GPL with the following exception:
  As a special exception, the copyright holders of this library give you permission
  to link this library with independent modules to produce an executable, regardless
  of the license terms of these independent modules, and to copy and distribute the
  resulting executable under terms of your choice, provided that you also meet, for
  each linked independent module, the terms and conditions of the license of that
  module. An independent module is a module which is not derived from or based on
  this library. If you modify this library, you may extend this exception to your
  version of the library, but you are not obligated to do so. If you do not wish
  to do so, delete this exception statement from your version.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Copyright (c) 2000-2006 RTJ Computing Pty. Ltd. All rights reserved.
***********************************************************************/
package lpc2xxx.hw;


/**
 * GPIO class implementing port functions of Philips LPC21xx devices.
 * LPC21xx can feature up to four ports of 32 general purpose I/O pins. 
 * These pins can be individually configured for different operational modes.<br>
 * <i>Note:</i>Please check the Philips LPC21xx documentation for detailed description of 
 * pin functions. Not all ports and pins may be available for a particular device.
 * 
 */
public class GPIO
{
    // pin numbers
    public final static int P0 =  0x1;
    public final static int P1 =  0x2;
    public final static int P2 =  0x4;
    public final static int P3 =  0x8;
    public final static int P4 =  0x10;
    public final static int P5 =  0x20;
    public final static int P6 =  0x40;
    public final static int P7 =  0x80;
    public final static int P8 =  0x100;
    public final static int P9 =  0x200;
    public final static int P10 = 0x400;
    public final static int P11 = 0x800;
    public final static int P12 = 0x1000;
    public final static int P13 = 0x2000;
    public final static int P14 = 0x4000;
    public final static int P15 = 0x8000;
    public final static int P16 = 0x10000;
    public final static int P17 = 0x20000;
    public final static int P18 = 0x40000;
    public final static int P19 = 0x80000;
    public final static int P20 = 0x100000;
    public final static int P21 = 0x200000;
    public final static int P22 = 0x400000;
    public final static int P23 = 0x800000;
    public final static int P24 = 0x1000000;
    public final static int P25 = 0x2000000;
    public final static int P26 = 0x4000000;
    public final static int P27 = 0x8000000;
    public final static int P28 = 0x10000000;
    public final static int P29 = 0x20000000;
    public final static int P30 = 0x40000000;
    public final static int P31 = 0x80000000;
    public final static int PALL = 0xFFFFFFFF;

    /** Identifies PORT0 */
    public final static int PORT0 = 0x00;
    /** Identifies PORT1 */
    public final static int PORT1 = 0x10;
    /** Identifies PORT2 */
    public final static int PORT2 = 0x20;
    /** Identifies PORT3 */
    public final static int PORT3 = 0x30;

    private int port;

    /**
     * Creates and instance of GPIO object on the specified port number.
     * @param port PORTn constant identifying the port.
     */
    public GPIO(int port)
    {
        this.port = port;
    }

    /**
     * Read the status of all pins on the port.
     * @return currents status all pins.
     */
    public int read()
    {
        return read(port);
    }

    /**
     * Specified pins will be set to <i>high</i> logic level. Only pins configured for
     * GPIO output function will be affected.
     * @param pins GPIO pins to set output value logic 1. Use P0-P31 constants. Multiple pins can 
     *             be combined together with <i>or</i> operator.
     */
    public void set(int pins)
    {
        set(port, pins);
    }

    /**
     * Specified pins will be set to <i>low</i> logic level. Only pins configured for
     * GPIO output function will be affected.
     * @param pins GPIO pins to set output value logic 0. Use P0-P31 constants. Multiple pins can 
     *             be combined together with <i>or</i> operator.
     */
    public void clear(int pins)
    {
        clear(port, pins);
    }

    /**
     * Method sets the output direction on the specified pins. 
     * @param pins GPIO pins to set the direction for output. Use P0-P31 constants. Multiple pins can 
     *             be combined together with <i>or</i> operator.
     */
    public void dirOut(int pins)
    {
        iodir(port, 0xFFFFFFFF, pins);
    }

    /**
     * Method sets the input direction on the specified pins. 
     * @param pins GPIO pins to set the direction for input. Use P0-P31 constants. Multiple pins can 
     *             be combined together with <i>or</i> operator.
     */
    public void dirIn(int pins)
    {
        iodir(port, ~pins, 0);
    }
    
    /**
     * Pin function configuration. AND and/or OR mask values can be used to enable
     * required functions on certain pins without affecting others.<br>
     * Please consult the Philips user's manual for detalis on configuring pin functions using
     * PINSEL0-2 registers.
     * @param pinsel number 0,1 or 2 identifying the PINSEL register.<br>
     *        PINSEL0 affects ports P0.0-15<br>
     *        PINSEL1 applies to P0.16-31<br>
     *        PINSEL2 is for P1.16-32
     * @param andMask AND mask that will be applied to selected PINSEL register
     * @param orMask OR mask that will be applied to selected PINSEL register
     */
    public native static void config(int pinsel, int andMask, int orMask);

     /**
     * Read the status of all pins on the port.
     * @param port port identifier, one of PORTn constants
     * @return currents status all pins.
     */
    private static native int read(int port);

    /**
     * Specified pins will be set to <i>high</i> logic level. Only pins configured for
     * GPIO output function will be affected.
     * @param port port identifier, one of PORTn constants
     * @param pins GPIO pins to set output value logic 1. Use P0-P31 constants. Multiple pins can 
     *             be combined together with <i>or</i> operator.
     */
    private static native void set(int port, int pins);

    /**
     * Specified pins will be set to <i>low</i> logic level. Only pins configured for
     * GPIO output function will be affected.
     * @param port port identifier, one of PORTn constants
     * @param pins GPIO pins to set output value logic 0. Use P0-P31 constants. Multiple pins can 
     *             be combined together with <i>or</i> operator.
     */
    private static native void clear(int port, int pins);

   /**
     * Method sets the direction for pins configured as GPIO. Logic 1 sets the pin for output, 
     * logic 0 sets the pin for input.
     * @param and AND mask to apply on IODIR register.
     * @param or OR mask to apply on IODIR register
     */
    private native static void iodir(int port, int and, int or);

}
